<?php

if (!defined('BASEPATH'))
     exit('No direct script access allowed');
require_once 'Matrix.php';

class Call_matrix {

     function set_svd($value) {
          $svd = new Matrix($value);
          return $svd;
     }

     /*
      * U = eigen vektor kiri
      */

     function nilai_svd_u($value,$k) {
          $term_frekuensi = $this->term_frekuensi($value);
          $U = $this->set_svd($term_frekuensi);
          $vector = $U->svd()->U;
          if ($k == null) {
               $k = count($vector[0]) - 1;
          }
          for ($a = 0; $a < count($vector); $a++) {
               for ($b = 0; $b < $k; $b++) {
                    if (($b % 2) != 0) {
                         $eigenvektor_kiri[$a][$b] = $vector[$a][$b] * -1;
                    }else
                         $eigenvektor_kiri[$a][$b] = $vector[$a][$b];
               }
          }
          return $eigenvektor_kiri;
     }

     /*
      * V = eigen vektor kanan
      */

     function nilai_svd_v($value, $k) {
          $term_frekuensi = $this->term_frekuensi($value);
          $V = $this->set_svd($term_frekuensi);
          $eigen = $V->svd()->V;
          if ($k == null) {
               $k = count($eigen[0]) - 1;
          }
          for ($a = 0; $a < count($eigen); $a++) {
               for ($b = 0; $b < $k; $b++) {
                    if (($b % 2) != 0) {
                         $eigenvektor_kanan[$a][$b] = $eigen[$a][$b] * -1;
                    }else
                         $eigenvektor_kanan[$a][$b] = $eigen[$a][$b];
               }
          }
          return $eigenvektor_kanan;
     }

     /*
      * S = singular value
      */

     function nilai_svd_s($value, $k) {

          $term_frekuensi = $this->term_frekuensi($value);

          $s = $this->set_svd($term_frekuensi);
          $singular = $s->svd()->s;

          if ($k == null) {
               $k = count($singular) - 1;
          }
          for ($a = 0; $a < $k; $a++) {
               for ($b = 0; $b < $k; $b++) {
                    if ($a == $b) {
                         $singular_value[$a][$b] = $singular[$a];
                    } else {
                         $singular_value[$a][$b] = 0;
                    }
               }
          }
          return $singular_value;
     }

     function inverse_array($value) {
          $i = $this->set_svd($value);
          $inverse = $i->inverse()->A;
          return $inverse;
     }

     function term_frekuensi($value) {
          $a = 0;
          foreach ($value as $row) {
               $b = 0;
               foreach ($row as $cols) {
                    $term_frekuensi[$a][$b] = $cols;
                    $b++;
               }
               $a++;
          }
          return $term_frekuensi;
     }

}